package com.sniff.atp.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.sniff.atp.constant.LoginConstant;
import com.sniff.atp.dto.ClientLoginDTO;
import com.sniff.atp.dto.ContentDTO;
import com.sniff.atp.dto.request.StockOutDTO;
import com.sniff.atp.entity.ToolsInfo;
import com.sniff.atp.manager.ToolsInfoMananager;
import com.sniff.atp.response.SniffResponse;
import com.sniff.atp.service.login.ClientLoginService;
import com.sniff.atp.service.wms.stock.WmsStockOutService;
import com.sniff.atp.utils.DBUtil;
import io.swagger.annotations.*;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: sniff-atp
 * @Author: ljj
 * @CreateTime: 2022/11/14 12:52
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/stockout")
@Api(tags = "出库模块")
public class StockOutCtroller {
    @Autowired
    private WmsStockOutService wmsStockOutService;
    @Autowired
    private ClientLoginService clientLoginService;
    @Autowired
    private ToolsInfoMananager toolsInfoMananager;

    @ApiOperation("发货单审核")
    @GetMapping("/examineGJFH")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "String",name = "deliveryTaskCode",value = "发货单号",required = false),
            @ApiImplicitParam(paramType = "String",name = "platformOrderNo",value = "订单编号",required = false),
            @ApiImplicitParam(paramType = "Integer",name = "deliverType",value = "发货类型(fba发货0,大包发货1,小包发货2)",required = true)})
    public Object examineGJFH(String deliveryTaskCode, Integer deliverType , String platformOrderNo){

        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(12L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        if(StringUtils.isEmpty(deliveryTaskCode) && StringUtils.isEmpty(platformOrderNo)){
            return SniffResponse.fail(999,"发货单号和订单编号不能同时为空！！");
        }

        //看有没有发货单号，没有发货单号的通过订单查询
        if(StringUtils.isNotEmpty(deliveryTaskCode)){
            wmsStockOutService.switchHeaderByWarehouse(LoginConstant.wmsLoginDTO,deliveryTaskCode);
        }else{
            wmsStockOutService.switchHeaderByWarehouseOrderNo(LoginConstant.wmsLoginDTO,platformOrderNo);
            deliveryTaskCode = platformOrderNo;
        }
        String res = wmsStockOutService.examineGJFH(LoginConstant.wmsLoginDTO, deliverType,deliveryTaskCode);
        return JSON.parse(res);
    }

    @ApiOperation("生成拣货任务")
    @GetMapping("/createPickTask")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "Integer",name = "deliveryType",value = "发货类型(fba发货0,大包发货1,小包发货2)",required = true),
            @ApiImplicitParam(paramType = "Integer",name = "pickType",value = "捡货类型(FBA拣货3,大件拣货0,小件单品1,小件多品2)",required = true),
            @ApiImplicitParam(paramType = "String",name = "deliveryTaskCodes",value = "发货单号,多个用逗号隔开",required = true)})
    @ApiOperationSupport(order = 2)
    public Object createPickTask(Integer deliveryType, Integer pickType, String... deliveryTaskCodes){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(13L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        wmsStockOutService.switchHeaderByWarehouse(LoginConstant.wmsLoginDTO,deliveryTaskCodes[0]);
        String res = wmsStockOutService.createPickTask(LoginConstant.wmsLoginDTO, deliveryType,pickType,deliveryTaskCodes);
        return JSON.parse(res);
    }

    @ApiOperation("根据发货单pda拣货")
    @GetMapping("/picking")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "String",name = "deliveryTaskCode",value = "发货单号",required = true),
            @ApiImplicitParam(paramType = "Integer",name = "deliverType",value = "发货类型(fba发货0,大包发货1,小包发货2)",required = true)})
    @ApiOperationSupport(order = 3)
    public Object picking(Integer deliverType,String deliveryTaskCode){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(14L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        wmsStockOutService.switchHeaderByWarehouse(LoginConstant.wmsLoginDTO,deliveryTaskCode);
        String res = wmsStockOutService.picking(LoginConstant.wmsLoginDTO, deliverType,deliveryTaskCode);
        return JSON.parse(res);
    }

    @ApiOperation("根据发货单打包完成")
    @GetMapping("/packing")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "String",name = "deliveryTaskCode",value = "发货单号",required = true),
            @ApiImplicitParam(paramType = "Integer",name = "deliverType",value = "发货类型(fba发货0,大包发货1,小包发货2)",required = true)})
    public Object packing(Integer deliverType,String deliveryTaskCode){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(15L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        wmsStockOutService.switchHeaderByWarehouse(LoginConstant.wmsLoginDTO,deliveryTaskCode);
        String res = wmsStockOutService.packing(LoginConstant.wmsLoginDTO, deliverType,deliveryTaskCode);
        return JSON.parse(res);
    }

    @ApiOperation("根据发货单发货完成")
    @GetMapping("/deliveryCompleted")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "String",name = "deliveryTaskCode",value = "发货单号",required = true),
            @ApiImplicitParam(paramType = "Integer",name = "deliverType",value = "发货类型(fba发货0,大包发货1,小包发货2)",required = true)})
    public Object deliveryCompleted(Integer deliverType,String deliveryTaskCode){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(16L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        wmsStockOutService.switchHeaderByWarehouse(LoginConstant.wmsLoginDTO,deliveryTaskCode);
        String res = wmsStockOutService.deliveryCompleted(LoginConstant.wmsLoginDTO, deliverType,deliveryTaskCode);
        return JSON.parse(res);
    }

    @ApiOperation("国际发货单生成并出库")
    @PostMapping("/stockOut")
    @ApiOperationSupport(order = 10)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "String",name = "shopProductSku",value = "平台sku",required = true),
            @ApiImplicitParam(paramType = "Integer",name = "num",value = "发货数量",required = true)})
    public Object stockOut(String username,String password,String shopProductSku,Integer num){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(17L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        ClientLoginDTO clientLoginDTO = clientLoginService.login(username, password);
        if ("1".equals(clientLoginDTO.getSystemSource())) {
            clientLoginService.changeLogin(clientLoginDTO);
        }
        JSONObject params= new JSONObject();
        params.put(shopProductSku,num);
        String res = wmsStockOutService.stockOut(clientLoginDTO,LoginConstant.wmsLoginDTO,params);
        return JSON.parse(res);
    }

    @ApiOperation("国际发货单出库")
    @PostMapping("/deliveryStockOut")
    @ApiOperationSupport(order = 13)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "String",name = "deliveryTaskCode",value = "国际发货单号",required = true),
            @ApiImplicitParam(paramType = "Integer",name = "operationNode",value = "1 审单完成; 2 生成拣货任务; 3 拣货完成; 4 打标完成; 5 打包完成; 6 发货完成",required = false)})

    public Object deliveryStockOut(String deliveryTaskCode, Integer operationNode){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(31L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        JSONObject jsonObject = wmsStockOutService.deliveryStockOut(LoginConstant.wmsLoginDTO, deliveryTaskCode, operationNode);

        //根据国际发货单号查询国际物流单号
        List<JSONObject> sql024 = DBUtil.select("WMS-根据国际发货单查询发货任务物流信息", new ContentDTO("",deliveryTaskCode));
        List<String> logisticsCodeList = sql024.stream()
                .map(obj -> obj.getString("logistics_code"))
                .collect(Collectors.toList());
        JSONObject data = new JSONObject();
        data.put("logisticsCodeList",new JSONArray(logisticsCodeList));
        jsonObject.put("data",data);
        return jsonObject;
    }

    @ApiOperation("给定一个待审核的d2c出库平台订单进行出库")
    @PostMapping("/stockOutD2c")
    @ApiOperationSupport(order = 11)
    public Object stockOutD2c(@RequestBody StockOutDTO stockOutDTO){
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(18L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        String res = "{\"code\":9999,\"msg\":\"订单号与发货类型必传\"}";


        if ((stockOutDTO.getDeliverType()!=null&&stockOutDTO.getPlatformOrderNo()!=null)&&(!"".equals(stockOutDTO.getPlatformOrderNo()))){

            //有发货单号的使用发货单号，没有发货单号的使用订单编号
            String orderNo = "";
            //看有没有发货单号，没有发货单号的通过订单查询
            if(StringUtils.isNotEmpty(stockOutDTO.getDeliveryTaskCode())){
                wmsStockOutService.switchHeaderByWarehouse(LoginConstant.wmsLoginDTO,stockOutDTO.getDeliveryTaskCode());
                orderNo = stockOutDTO.getDeliveryTaskCode();
            }else{
                wmsStockOutService.switchHeaderByWarehouseOrderNo(LoginConstant.wmsLoginDTO,stockOutDTO.getPlatformOrderNo());
                orderNo = stockOutDTO.getPlatformOrderNo();
            }

//            wmsStockOutService.switchHeaderByWarehouse(LoginConstant.wmsLoginDTO,stockOutDTO.getPlatformOrderNo());
            res = wmsStockOutService.stockOutD2cStep(LoginConstant.wmsLoginDTO,stockOutDTO);
        }
        return JSON.parse(res);
    }


    @ApiOperation("删除库存")
    @PostMapping("/deleteShopSku")
    @ApiOperationSupport(order = 12)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "String",name = "productskus",value = "sku",required = true),
            @ApiImplicitParam(paramType = "String",name = "customerShopId",value = "店铺id",required = false)})
    public Object deleteShopSku(String productskus,String customerShopId){

        String res = wmsStockOutService.deleteShopSku(LoginConstant.wmsLoginDTO,productskus,customerShopId);
        return JSON.parse(res);
    }

    @ApiOperation("查询国际发货单状态")
    @GetMapping("/getDeliveryTaskInfo")
    @ApiOperationSupport(order = 13)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "String",name = "deliveryTaskCode",value = "发货单号",required = true)})
    public Object getDeliveryTaskInfo(String deliveryTaskCode){
        JSONObject res = wmsStockOutService.getDeliveryTaskInfo(deliveryTaskCode);
        return res;
    }


}
